/*
 * Level.cpp
 *
 *  Created on: Jan 1, 2014
 *      Author: nick
 */

#include "Level.h"

void clip_tiles() {
	//Clip the sprite sheet
	for( int i = 0; i < TILE_SPRITES; ++i )
	{
		clips[i] = new SDL_Rect();
	}

	clips[TILE_BRICK]->x = 0;
	clips[TILE_BRICK]->y = 0;
	clips[TILE_BRICK]->w = TILE_WIDTH;
	clips[TILE_BRICK]->h = TILE_HEIGHT;

	clips[TILE_WALL]->x = 0;
	clips[TILE_WALL]->y = 80;
	clips[TILE_WALL]->w = TILE_WIDTH;
	clips[TILE_WALL]->h = TILE_HEIGHT;
}

bool set_tiles(Sprite *tiles[]) {
	//The tile offsets
	int x = 0, y = 0;

	//Open the map
	std::ifstream map("/home/nick/code/Bin/Data/lazy.map");

	//If the map couldn't be loaded
	if (map == NULL) {
		return false;
	}

	//Initialize the tiles
	for (int t = 0; t < TOTAL_TILES; t++) {
		//Determines what kind of tile will be made
		int tileType = -1;

		//Read tile from map file
		map >> tileType;

		//If the was a problem in reading the map
		if (map.fail() == true) {
			//Stop loading map
			map.close();
			return false;
		}

		//If the number is a valid tile number
		if ((tileType >= 0) && (tileType < TILE_SPRITES)) {
			tiles[t] = new Tile(x, y, tileType);
		}
		//If we don't recognize the tile type
		else {
			//Stop loading map
			map.close();
			return false;
		}

		//Move to next tile spot
		x += TILE_WIDTH;

		//If we've gone too far
		if (x >= LEVEL_WIDTH) {
			//Move back
			x = 0;

			//Move to the next row
			y += TILE_HEIGHT;
		}
	}

	//Close the file
	map.close();

	//If the map was loaded fine
	return true;
}

